package com.zhoug.zmui.ratio;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.zmui.R;

/**
 * @Author 35574
 * @Date 2020/7/30
 * @Description
 */
public class RatioViewHelper {
    private static final String TAG = ">>>RatioViewHelper";
    private static final boolean DEBUG = true;
    private float ratio;

    /**
     * 解析属性
     * @param context
     * @param attrs
     */
    public void parseAttr(Context context,AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RatioViewHelper, 0, 0);
        int ratioWidth = a.getInt(R.styleable.RatioViewHelper_zmui_ratioWidth, 0);
        int ratioHeight = a.getInt(R.styleable.RatioViewHelper_zmui_ratioHeight, 0);
        a.recycle();
        if (ratioWidth > 0 && ratioHeight > 0) {
            ratio = (float) ratioWidth / (float) ratioHeight;
        } else {
            ratio = 0;
        }
        if (DEBUG) {
            LogUtils.d(TAG, "RatioViewHelper:ratio=" + ratio);
        }
    }

    /**
     * 测量 在super.onMeasure(widthMeasureSpec,heightMeasureSpec);之后调用
     * 返回值不为null则重新测量 super.onMeasure(measureSpecs[0], measureSpecs[1]);;
     * @param view
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     * @return measureSpecs
     */
    public int[] measureSpec(View view, int widthMeasureSpec, int heightMeasureSpec) {
        if (ratio > 0) {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
                if (DEBUG) {
                    LogUtils.d(TAG, "onMeasure:宽高确定，比例无效化");
                }

                return null;
            } else if (widthMode == MeasureSpec.EXACTLY) {
                if (DEBUG) {
                    LogUtils.d(TAG, "onMeasure:宽度确定,计算高度");
                }
                int height = (int) (widthSize / ratio);
                int[] measureSpecs = new int[2];
                measureSpecs[0] = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
                measureSpecs[1] = MeasureSpec.makeMeasureSpec(Math.min(height, heightSize), MeasureSpec.EXACTLY);
                return measureSpecs;
            } else if (heightMode == MeasureSpec.EXACTLY) {
                if (DEBUG) {
                    LogUtils.d(TAG, "onMeasure:高度确定,计算宽度");
                }
                int width = (int) (heightSize * ratio);
                int[] measureSpecs = new int[2];
                measureSpecs[0] = MeasureSpec.makeMeasureSpec(Math.min(width, widthSize), MeasureSpec.EXACTLY);
                measureSpecs[1] = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
                return measureSpecs;
            } else {
                if (DEBUG) {
                    LogUtils.d(TAG, "onMeasure:宽高不确定");
                }
                int measuredWidth = view.getMeasuredWidth();
                int measuredHeight = view.getMeasuredHeight();
                //以测量宽度计算高度
                int height = (int) (measuredWidth / ratio);
                //以测量高度计算宽度
                int width = (int) (measuredHeight * ratio);
                if (height >= measuredHeight && height <= heightSize) {
                    int[] measureSpecs = new int[2];
                    measureSpecs[0] = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
                    measureSpecs[1] = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
                    return measureSpecs;
                } else if (width >= measuredWidth && width <= widthSize) {
                    int[] measureSpecs = new int[2];
                    measureSpecs[0] = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
                    measureSpecs[1] = MeasureSpec.makeMeasureSpec(measuredHeight, MeasureSpec.EXACTLY);
                    return measureSpecs;
                }
            }
        }
        return null;
    }


    public float getRatio() {
        return ratio;
    }

    public void setRatio(float ratio) {
        this.ratio = ratio;
    }

}
